package site.jlopen.kuscia.exception;

import site.jlopen.kuscia.errorcode.ErrorCode;

public final class SdkBaseException extends RuntimeException {
	/**
	 * 
	 */
	private static final long serialVersionUID = 9102820623814148719L;
	/**
	 * error code
	 */
	private final ErrorCode errorCode;
	/**
	 * error args
	 */
	private final String[] args;

	/**
	 * Fill Exception
	 *
	 * @param errorCode
	 * @param cause
	 * @param args
	 */
	private SdkBaseException(ErrorCode errorCode, Throwable cause, String... args) {
		super(args != null && args.length > 0 ? args[0] : "", cause);
		this.errorCode = errorCode;
		this.args = args;
	}
	
	private SdkBaseException(String errMsg, Throwable cause) {
		super(errMsg, cause);
		this.errorCode = null;
		this.args = null;
	}

	/**
	 * Build Exception with args
	 *
	 * @param errorCode
	 * @param args
	 * @return exception
	 */
	public static SdkBaseException of(ErrorCode errorCode, String... args) {
		return new SdkBaseException(errorCode, null, args);
	}

	/**
	 * Build Exception with args
	 *
	 * @param errorCode
	 * @param args
	 * @return exception
	 */
	public static SdkBaseException of(ErrorCode errorCode, Throwable cause, String... args) {
		return new SdkBaseException(errorCode, cause, args);
	}

	/**
	 * Build Exception with cause
	 *
	 * @param errorCode
	 * @param cause
	 * @return exception
	 */
	public static SdkBaseException of(ErrorCode errorCode, Throwable cause) {
		return new SdkBaseException(errorCode, cause, cause.getMessage());
	}
	
	public static SdkBaseException of(String errorMsg, Throwable cause) {
		return new SdkBaseException(errorMsg, cause);
	}

	/**
	 * @return error code
	 */
	public ErrorCode getErrorCode() {
		return errorCode;
	}

	/**
	 * @return error args
	 */
	public String[] getArgs() {
		return args;
	}
}